package net.wofly.cashier.reservation.web;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import net.wofly.cashier.reservation.domain.Reservation;
import net.wofly.cashier.reservation.service.IReservationService;
import net.wofly.cashier.reservation.validator.ReservationValidator;
import net.wofly.common.persistence.PageWrapper;
import net.wofly.common.util.ReturnBean;
import net.wofly.common.web.BaseController;

/**
 * @author lign
 * @version 1.0
 * @created 17-十月-2017 23:09:08
 */
@RestController
@RequestMapping(value = "/api/reservation")
@Api(description = "预定接口")
public class ReservationController extends BaseController {

	@Autowired
	IReservationService iReservationService;

	/**
	 * 新增预约
	 * <ul>
	 * 	<li>按预约日期及就餐类型自动增加预约顺序码</li>
	 * 	<li>验证employeeID用户权限</li>
	 * 	<li></li>
	 * </ul>
	 *
	 * @param reservation
	 */
	@ApiOperation(value = "新增预约", notes = "新增预约")
	@ApiImplicitParam(name = "reservation", value = "预约实体", required = true, dataType = "Reservation")
	@RequestMapping(value = "/addReservation", method = {RequestMethod.POST})
	public ReturnBean<Reservation> addReservation(@RequestBody Reservation reservation){
		ReturnBean<Reservation> returnBean;
		String errorMessage = ReservationValidator.validateAddReservationParams(reservation);
		if (StringUtils.isNotEmpty(errorMessage)) {
			returnBean = new ReturnBean<>(-1, errorMessage);
		} else {
			returnBean = iReservationService.addReservation(reservation);
		}
		return returnBean;
	}

	/**
	 * 取消预约
	 * <ul>
	 * 	<li>验证employeeID用户权限</li>
	 * </ul>
	 * <ul>
	 * 	<li>将状态置为预约取消</li>
	 * 	<li></li>
	 * </ul>
	 *
	 * @param reservationID
	 * @param employeeID
	 */
	@ApiOperation(value = "取消预约", notes = "取消预约")
	@ApiImplicitParams({@ApiImplicitParam(name = "reservationID", value = "预定ID", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "employeeID", value = "操作员工ID", required = true, dataType = "String", paramType = "form")})
	@RequestMapping(value = "/cancelReservation", method = {RequestMethod.POST})
	public ReturnBean<String> cancelReservation(String reservationID, String employeeID){
		ReturnBean<String> returnBean;
		String errorMessage = ReservationValidator.validateCancelReservationParams(reservationID, employeeID);
		if (StringUtils.isNotEmpty(errorMessage)) {
			returnBean = new ReturnBean<>(-1, errorMessage);
		} else {
			returnBean = iReservationService.cancelReservation(reservationID, employeeID);
		}
		return returnBean;
	}

	/**
	 * 分页获取所有预约
	 * <ul>
	 * 	<li>reservationMealTime=-1 && reservationMealType: =-1时，获取所有预约</li>
	 * 	<li>验证当前用户是否有权限操作</li>
	 * 	<li></li>
	 * </ul>
	 *
	 * @param employeeID
	 * @param reservationMealTime
	 * @param reservationMealType
	 */
	@ApiOperation(value = "分页获取兑换库存", notes = "分页获取兑换库存")
	@ApiImplicitParams({@ApiImplicitParam(name = "employeeID", value = "操作员工ID", required = false, dataType = "String", paramType = "query"),
			@ApiImplicitParam(name = "reservationMealTime", value = "预约就餐时间", required = false, dataType = "long", paramType = "query"),
			@ApiImplicitParam(name = "reservationMealType", value = "预约就餐类型", required = false, dataType = "int", paramType = "query"),
			@ApiImplicitParam(name = "pageNum", value = "第几页", required = false, dataType = "int", paramType = "query"),
			@ApiImplicitParam(name = "pageSize", value = "每页多少条", required = false, dataType = "int", paramType = "query")})
	@RequestMapping(value = "/getAllReservation", method = {RequestMethod.GET})
	public ReturnBean<PageWrapper<Reservation>> getAllReservation(@RequestParam(defaultValue = "1", name = "pageNum") Integer pageNum,
																  @RequestParam(defaultValue = "20", name = "pageSize") Integer pageSize,
																  String employeeID, Long reservationMealTime, Integer reservationMealType){
		ReturnBean<PageWrapper<Reservation>> returnBean;
		String errorMessage = ReservationValidator.validateGetAllReservationParams(employeeID, reservationMealTime, reservationMealType);
		if (StringUtils.isNotEmpty(errorMessage)) {
			returnBean = new ReturnBean<>(-1, errorMessage);
		} else {
			returnBean = iReservationService.getAllReservation(pageNum, pageSize, employeeID, reservationMealTime, reservationMealType);
		}
		return returnBean;
	}

	/**
	 * 就餐
	 * <ul>
	 * 	<li>修改预约状态</li>
	 * 	<li>同时，在订单表中增加一条点餐</li>
	 * 	<li>验证employeeID用户权限</li>
	 * 	<li></li>
	 * </ul>
	 *
	 * @param reservationID
	 * @param employeeID
	 */
	@ApiOperation(value = "就餐", notes = "就餐")
	@ApiImplicitParams({@ApiImplicitParam(name = "reservationID", value = "预定ID", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "employeeID", value = "操作员工ID", required = true, dataType = "String", paramType = "form")})
	@RequestMapping(value = "/repast", method = {RequestMethod.POST})
	public ReturnBean<String> repast(String reservationID, String employeeID){
		ReturnBean<String> returnBean;
		String errorMessage = ReservationValidator.validateRepastParams(reservationID, employeeID);
		if (StringUtils.isNotEmpty(errorMessage)) {
			returnBean = new ReturnBean<>(-1, errorMessage);
		} else {
			returnBean = iReservationService.repast(reservationID, employeeID);
		}
		return returnBean;
	}

	/**
	 * 修改预约
	 * <ul>
	 * 	<li>按新预约日期及就餐类型自动增加预约顺序码</li>
	 * 	<li>原预约日期及就餐类型的预约顺序码放弃。</li>
	 * 	<li>验证employeeID用户权限</li>
	 * 	<li></li>
	 * </ul>
	 *
	 * @param reservation
	 */
	@ApiOperation(value = "修改预约", notes = "修改预约")
	@ApiImplicitParam(name = "reservation", value = "预约实体", required = true, dataType = "Reservation")
	@RequestMapping(value = "/updateReservation", method = {RequestMethod.POST})
	public ReturnBean<Reservation> updateReservation(@RequestBody Reservation reservation){
		ReturnBean<Reservation> returnBean;
		String errorMessage = ReservationValidator.validateUpdateReservationParams(reservation);
		if (StringUtils.isNotEmpty(errorMessage)) {
			returnBean = new ReturnBean<>(-1, errorMessage);
		} else {
			returnBean = iReservationService.updateReservation(reservation);
		}
		return returnBean;
	}

}